/* 
 * File:   ListadoNiveles.cpp
 * Author: agu
 * 
 * Created on 7 de junio de 2014, 16:41
 */

#include "editor_ListadoNiveles.h"
#include <iostream>

ListadoNiveles::ListadoNiveles()
: m_VBox(Gtk::ORIENTATION_VERTICAL),
m_Button_Back("Volver") {
    this->loadWidgets();
    this->connectWidgets();
}

void ListadoNiveles::loadWidgets() {
    set_title("Listado de niveles");
    set_border_width(5);
    set_default_size(400, 600);
    set_position(Gtk::WIN_POS_CENTER_ALWAYS);
    add(m_VBox);

    //Add the TreeView, inside a ScrolledWindow, with the button underneath:
    m_ScrolledWindow.add(m_TreeView);

    //Only show the scrollbars when they are necessary:
    m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

    m_VBox.pack_start(m_ScrolledWindow);
    m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);

    m_ButtonBox.pack_start(m_Button_Back, Gtk::PACK_SHRINK);
    m_ButtonBox.set_border_width(5);
    m_ButtonBox.set_layout(Gtk::BUTTONBOX_CENTER);
    //Create the Tree model:
    m_refTreeModel = Gtk::ListStore::create(m_Columns);
    m_TreeView.set_model(m_refTreeModel);
    addColumns();
    this->edit = false;

}

void ListadoNiveles::connectWidgets() {

    m_Button_Back.signal_clicked().connect(sigc::mem_fun(*this,
            &ListadoNiveles::back));
    m_TreeView.signal_row_activated().connect(sigc::mem_fun(*this,
            &ListadoNiveles::select));
    this->signal_hide().connect(sigc::mem_fun(this, &ListadoNiveles::close));
    this->signal_show().connect(sigc::mem_fun(this, &ListadoNiveles::loadRows));
    show_all_children();
}

ListadoNiveles::~ListadoNiveles() {
}

void ListadoNiveles::loadRows() {
    //Fill the TreeView's model
    m_refTreeModel->clear();
    Gtk::TreeModel::Row row;
    for (unsigned int i = 0; i < this->levels->size(); i++) {
        row = *(m_refTreeModel->append());
        row[m_Columns.m_col_id] = this->levels->at(i).getId();
        row[m_Columns.m_col_name] = this->levels->at(i).getName();
    }

}

void ListadoNiveles::addColumns() {
    //Add the TreeView's view columns:
    //This number will be shown with the default numeric formatting.
    m_TreeView.append_column("ID", m_Columns.m_col_id);
    m_TreeView.append_column("Name", m_Columns.m_col_name);

}

void ListadoNiveles::setLevels(vector<Nivel>* levels) {
    this->levels = levels;
}

void ListadoNiveles::setWindowsBack(Gtk::Window *windowBack) {
    this->windowsBack = windowBack;
}

void ListadoNiveles::run() {
    //this->loadRows();
    this->show();
}

void ListadoNiveles::close() {
    this->hide();
}

void ListadoNiveles::back() {
    this->set_visible(false);
    this->windowsBack->set_visible(true);
}

void ListadoNiveles::editable() {
    this->edit = true;
}

void ListadoNiveles::select(const Gtk::TreeModel::Path& path,
        Gtk::TreeViewColumn* /* column */) {
    Gtk::TreeModel::iterator iter = m_refTreeModel->get_iter(path);
    if (iter) {
        Gtk::TreeModel::Row row = *iter;
        this->set_visible(false);
        int indice = -1;
        for (unsigned int i = 0; i < this->levels->size(); i++) {
            if (this->levels->at(i).getId() == row[m_Columns.m_col_id]) {
                indice = i;
            }

        }
        levelView.setWindowsBack(this);
        levelView.setLevel(&this->levels->at(indice));
        if (!this->edit)
            levelView.run();
        else
            levelView.runLoadedLevel();
    }
}